package cn.wenhe9.novel.exception;

import cn.wenhe9.novel.constant.ResultCodeEnum;
import cn.wenhe9.novel.result.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 通用的异常处理器
 * @author DuJinliang
 * 2022/5/20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理数据校验异常
     * */
    @ExceptionHandler(BindException.class)
    public ResultResponse<Void> handlerBindException(BindException e){
        log.error(e.getMessage(),e);
        return ResultResponse.fail(ResultCodeEnum.USER_REQUEST_PARAM_ERROR);
    }

    /**
     * 处理业务异常
     * */
    @ExceptionHandler(NovelException.class)
    public ResultResponse<Void> handlerBusinessException(NovelException e){
        log.error(e.getMessage(),e);
        return ResultResponse.fail(e.getResultCodeEnum());
    }

    /**
     * 处理系统异常
     * */
    @ExceptionHandler(Exception.class)
    public ResultResponse<Void> handlerException(Exception e){
        log.error(e.getMessage(),e);
        return ResultResponse.error();
    }

}
